package cn.edu.suda.catalina.health;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import cn.edu.suda.catalina.util.SharedPreferencesUtil;

public class RiskActivity extends AppCompatActivity {

    private Boolean misScrolled = true;

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_risk);

        SharedPreferencesUtil.clearSharedPreferences(this);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

                switch (state) {
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        misScrolled = false;
                        break;
                    case ViewPager.SCROLL_STATE_SETTLING:
                        misScrolled = true;
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        if (mViewPager.getCurrentItem() == mViewPager.getAdapter().getCount() - 1 && !misScrolled) {
                            startActivity(new Intent(RiskActivity.this, ResultActivity.class));
                            finish();
                        } else if (mViewPager.getCurrentItem() == 0 && !misScrolled) {
                            finish();
                        } else {}
                        misScrolled = true;
                        break;
                }

            }
        });

    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            switch (position) {
                case 0:
                    return OneFragment.newInstance();
                case 1:
                    return TwoFragment.newInstance();
                case 2:
                    return ThreeFragment.newInstance();
                case 3:
                    return FourFragment.newInstance();
                case 4:
                    return FiveFragment.newInstance();
                case 5:
                    return SixFragment.newInstance();
                case 6:
                    return SevenFragment.newInstance();
                case 7:
                    return EightFragment.newInstance();
                case 8:
                    return NineFragment.newInstance();
                case 9:
                    return TenFragment.newInstance();
                case 10:
                    return ElevenFragment.newInstance();
                case 11:
                    return TwelveFragment.newInstance();
                case 12:
                    return ThirteenFragment.newInstance();
            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 13 total pages.
            return 13;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
                case 3:
                    return "SECTION 3";
                case 4:
                    return "SECTION 1";
                case 5:
                    return "SECTION 2";
                case 6:
                    return "SECTION 3";
                case 7:
                    return "SECTION 3";
                case 8:
                    return "SECTION 1";
                case 9:
                    return "SECTION 2";
                case 10:
                    return "SECTION 3";
                case 11:
                    return "SECTION 3";
                case 12:
                    return "SECTION 3";
            }
            return null;
        }
    }
}
